<template>
  <SysDialog draggable :title="dialog.title" :visible="dialog.visible" :width="dialog.width" :height="dialog.height"
    @onClose="onClose" @onConfirm="confirm">
    <template v-slot:content>
      <!-- 内容组件 -->
      <el-form :model="dialogModel" ref="addRoleForm" :rules="rules" label-width="120px" size="default">
        <el-form-item prop="name" label="角色名称">
          <el-input v-model="dialogModel.name" clearable></el-input>
        </el-form-item>
        <el-form-item prop="description" label="角色描述">
          <el-input v-model="dialogModel.description" clearable></el-input>
        </el-form-item>
        <el-form-item prop="deptId" label="上级部门">
          <el-select v-model="dialogModel.deptId" placeholder="请选择上级部门" clearable size="default">
            <el-option v-for="item in roleTableDate" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item prop="state" label="角色状态">
            <el-select v-model="dialogModel.state" clearable placeholder="请选择角色状态" size="default">
            <el-option label="启用" :value="0" />
            <el-option label="禁用" :value="1" />
          </el-select>
        </el-form-item>
      </el-form>
    </template>
  </SysDialog>
</template>

<script setup lang='ts'>
import { ref } from 'vue'
import { ElForm } from 'element-plus'
import SysDialog from '@/components/SysDialog.vue';
import useDialog from '@/hooks/useDialog';
import useInstance from '@/hooks/useInstance';
import depeModel from '@/composables/03organization/03role/roleEditModel'
import { EditType, Title } from '@/type/BaseEnum'
// 使用
const { dialog, onClose, onShow } = useDialog()
const { global, proxy } = useInstance();
const { rules, dialogModel } = depeModel()
const addRoleForm = ref<InstanceType<typeof ElForm>>()
// 父级数据
const props = defineProps({
  roleTableDate: {
    type: Array<any>,
    default: () => []
  }
})
// 保存
const emit = defineEmits(['save'])
const confirm = () => {
  // 数据校验
  addRoleForm?.value?.validate((isOk) => {
    if (isOk) {
      console.log('数据校验');
      emit('save', dialogModel)
      onClose()
    }
  })
}
const show = (type: string, row?: any) => {
  console.log('show');
  dialog.width = 650
  dialog.height = 250
  // 动态
  dialog.title = type == EditType.ADD ? Title.ADD : Title.EDIT
  onShow()
  global.$resetForm(addRoleForm.value, dialogModel)
  if (type === EditType.EDIT) {
    global.$objCopy(row, dialogModel)
  }
}
defineExpose({
  show
})
</script>
<style scoped lang='scss'></style>